/**
 * @file JSONNode.cpp
 */
#include "JSONNode.h"
#include "../common/UtilFuncT.h"
#include "../lang/Primitive.h"
#include "JSONException.h"

using namespace cppflib::collections::tree;
using namespace cppflib::lang;

namespace cppflib
{

namespace json
{

/**
 *  JSONObject and JSONArray have no name
 */
JSONNode::JSONNode(void) : MCTreeNode(new CFString())
{
}

JSONNode::~JSONNode(void)
{
}

JSONNode::valuetype_t JSONNode::GetValueType(BaseObject *pVal)
{
   if (pVal == NULL) return JSONNode::JVL_NULL;
   if (cppflib::InstanceOf<CFString>(pVal)) return JSONNode::JVL_STRING;
   if (cppflib::InstanceOf<Int32>(pVal)) return JSONNode::JVL_INT32;
   if (cppflib::InstanceOf<Int64>(pVal)) return JSONNode::JVL_INT64;
   if (cppflib::InstanceOf<Double>(pVal)) return JSONNode::JVL_DOUBLE;
   if (cppflib::InstanceOf<Boolean>(pVal)) return JSONNode::JVL_BOOLEAN;
   if (cppflib::InstanceOf<JSONObject>(pVal)) return JSONNode::JVL_OBJECT;
   if (cppflib::InstanceOf<JSONArray>(pVal)) return JSONNode::JVL_ARRAY;
   return JSONNode::JVL_NONE; // unknown
}

MCTreeNode * JSONNode::GetChild(u32_t index)
{
   return MCTreeNode::GetChild(index);
}

MCTreeNode * JSONNode::GetChild(const BaseObject &key)
{
   throw JSONException(_S("Not implemented by json::JSONNode"));
}

bool JSONNode::AddChild(MCTreeNode * pChild)
{
   if (cppflib::InstanceOf<JSONNode>(pChild)) {
      return MCTreeNode::AddChild(pChild);
   }

   throw JSONException(_S("argument is not an instance of json::JSONNode"));
}

bool JSONNode::DeleteChild(const BaseObject &key)
{
   throw JSONException(_S("Not implemented by json::JSONNode"));
}

bool JSONNode::IsAncestor(MCTreeNode *pNode)
{
   if (cppflib::InstanceOf<JSONNode>(pNode)) {
      return MCTreeNode::IsAncestor(pNode);
   }

   throw JSONException(_S("argument is not an instance of json::Node"));
}

bool JSONNode::Move(MCTreeNode *pNode)
{
   if (cppflib::InstanceOf<JSONNode>(pNode)) {
      return MCTreeNode::Move(pNode);
   }

   throw JSONException(_S("argument is not an instance of json::Node"));
}

void JSONNode::DetachFromTree()
{
   throw JSONException(_S("Not implemented by json::JSONNode"));
}


} // end of namespace json

} // end of namespace cppflib
